package cn.sjx.graduate.web.controller;

import cn.sjx.graduate.domain.Employee;
import cn.sjx.graduate.domain.StaffPayroll;
import cn.sjx.graduate.qo.StaffTypeQuery;
import cn.sjx.graduate.service.IRedisService;
import cn.sjx.graduate.service.IStaffPayrollService;
import cn.sjx.graduate.util.JsonResult;
import cn.sjx.graduate.util.TokenUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author bo-feifei
 * @since 2021-11-07
 */
@Slf4j
@RestController
@RequestMapping("/staff/payroll")
@Api(tags = "工资明细、发放信息接口")
public class StaffPayrollController {

    @Autowired
    private IStaffPayrollService staffPayrollService;
    @Autowired
    private IRedisService redisService;

    @PostMapping("/query")
    @ApiOperation("首页展示工资数据")
    public JsonResult<IPage<StaffPayroll>> query(@RequestBody StaffTypeQuery qo, HttpServletRequest request) {
        String token = TokenUtil.getToken(request);
        Employee employee = redisService.getUserByToken(token);
        if (employee == null) {
            return JsonResult.error("当前未登录用户");
        }
        IPage<StaffPayroll> page = staffPayrollService.queryPage(qo,employee);
    //todo 总数不对
        return JsonResult.success(page);
    }

    @DeleteMapping("/delete")
    public JsonResult<?> delete(@RequestParam Long id) {
        if (null == id) {
            return JsonResult.error("参数不能为空");
        }
        staffPayrollService.removeById(id);
        return JsonResult.success();
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("保存/修改相应信息")
    public JsonResult<?> saveOrUpdate(@RequestBody StaffPayroll staffPayroll) {
        if (null == staffPayroll) {
            return JsonResult.error("参数不能为空");
        }
        if (null == staffPayroll.getId()) {
            staffPayrollService.save(staffPayroll);
        } else {
            staffPayrollService.updateById(staffPayroll);
        }

        return JsonResult.success();
    }

    /**
     * 工资发放接口
     *
     * @param staffId
     * @return
     */
    @PostMapping("/payroll")
    @ApiOperation("工资发放接口")
    public JsonResult<?> payroll(@RequestParam Long staffId) {
        staffPayrollService.payroll(staffId);
        return JsonResult.success();
    }
}

